<?php
/*
	
*/
defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;

$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
if($_RQ['op']=='list'){
	$return['ischecklogin']=cfc('core')->checklogin('','',true);
	//超时未支付订单取消
	$list=pdo_getall('core_account_message',array('uniacid'=>$_SESSION['uniacid'],'status'=>'0','type'=>1));
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	//查询进入该页面的客户所有订单
	$params=array('uid'=>$_SESSION['uid']?:-1,'uniacid'=>$_SESSION['uniacid'],'type'=>1);
	if(isset($_RQ['status']) && $_RQ['status'] != 10){
		$params['status']=intval($_RQ['status']);
		$return['status']=$_RQ['status'];
	}else{
		$return['status']=10;
	}
	$order_list=pdo_getslice('core_account_message',$params,array($pindex,$psize),$total,$fields,'','createtime desc');
	if($order_list){
		foreach($order_list as &$item){
			$user=model('core/member')->detail($item['uid'],array('credit1','credit2','mobile','groupid','avatar','nickname'));
			$item['nickname']=$user['nickname'];
			$item['mobile']=$user['mobile'];
			$item['data']=json_decode($item['data'],true);
			foreach($item['data'] as &$v){
				if($v['type']==5 && $v['data']){
					$v['data'] = explode(";", $v['data']);
					foreach($v['data'] as $k=>&$i){
						if(empty($v['data'][$k])){
							unset($v['data'][$k]);
						}
					}
				}
			}
			$item['createtime']=date('Y-m-d H:i:s', $item['createtime']);		
			$item['fee']=pdo_getcolumn('core_paylog',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['pay_id']),'fee');
			if($item['fee']>0){
				if($item['status']==0){
					$item['statustitle']='待付款';
				}elseif($item['status']==1){
					$item['statustitle']='已付款';
				}elseif($item['status']==2){
					$item['statustitle']='待处理';
				}elseif($item['status']==3){
					$item['statustitle']='已取消';
				}elseif($item['status']==4){
					$item['statustitle']='已完成';
				}
			}else{
				$item['fee']=0.00;
				if($item['status']==0){
					$item['statustitle']='待处理';
				}elseif($item['status']==1){
					$item['statustitle']='已处理';
				}elseif($item['status']==2){
					$item['statustitle']='已处理';
				}elseif($item['status']==3){
					$item['statustitle']='已取消';
				}elseif($item['status']==4){
					$item['statustitle']='已完成';
				}
			}
			
		}
	}
	$return['list']=$order_list;
	exi($return);
}
//订单详情
if($_RQ['op']=='detail'){
	if(!$_RQ['id']){
	exi('订单不存在，请重新选择','error');
	}
	$order=pdo_get('core_account_message',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	$order['package']=json_decode($order['package']);
	//用户信息
	$member=model('core/member')->detail($order['uid'],array('credit1','credit2','mobile','groupid','avatar','nickname'));
	$order['paytype']=pdo_getcolumn('core_paylog',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['pay_id']),'pay_type');
	$order['paytype']=get_pay_types()[$order['paytype']];
	$order['fee']=pdo_getcolumn('core_paylog',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['pay_id']),'fee');
	$order['fee']=$order['fee']?:0.00;
	$order['data']=json_decode($order['data'],true);
	foreach($order['data'] as &$v){
				if($v['type']==5 && $v['data']){
					$v['data'] = explode(";", $v['data']);
					foreach($v['data'] as $k=>&$i){
						if(empty($v['data'][$k])){
							unset($v['data'][$k]);
						}
					}
				}
	}
	$return['order']=$order;
	$return['member']=$member;
	exi($return);
}
if($_RQ['op']=='receive'){
	if(!$_RQ['id']){
		exi('订单不存在，请重新选择订单','error');
	}
	pdo_update('core_account_message',array('status'=>'4'),array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	exi('确认服务成功');
}
if($_RQ['op']=='del'){
	if(!$_RQ['id']){
		exi('订单不存在，请重新选择订单','error');
	}
	pdo_delete('core_account_message',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	exi('删除预约成功');
}
if($_RQ['op']=='pay'){
	$id= intval($_RQ['id']);
	if(!$id){
		exi('表单不存在，请重新选择表单','error');
	}
	$pay_id=pdo_getcolumn('core_account_message',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id),'pay_id');
	exi($pay_id);
}
if($_RQ['op']=='post'){
	//$posttype=1,多表单提交
	$posttype=$_RQ['posttype'];
	$postdata=get_postdata();
	$formid = 'id'.$postdata['pageid'].'i'.$postdata['index'];
	$msglog = pdo_get('core_account_message',array('uniacid'=>$_SESSION['uniacid'],'uid'=>$_SESSION['uid'],'formid'=>$formid));
	if(!empty($msglog) && !$posttype){
		$return['error'] =1;
		$return['message'] = '您已经提交过了！';
		exi($return);
	}
	$pageid=intval($postdata['pageid']);
	if($pageid){
		$id=pdo_getcolumn('core_account_web_tpl',array('id'=>$pageid,'uniacid'=>$_SESSION['uniacid']),'web_id');
	}
	$title=pdo_getcolumn('core_mobile_web',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']),'title');
	if($pageid){
		$tpls=pdo_get('core_account_web_tpl',array('id'=>$pageid,'uniacid'=>$_SESSION['uniacid']),array('setting'));
	}else{
		$tpls=pdo_get('core_account_web_tpl',array('is_open'=>1,'web_id'=>$id,'uniacid'=>$_SESSION['uniacid']),array('setting'));
	}
	$setting=json_decode($tpls['setting'],true);
	$fee=intval($setting['fee']);
	$data=array(
			'title'=>$title.'自定义表单',
			'data'=>ijson_encode($postdata['fileddata']),
			'posttype'=>$posttype,
			'plugin_name'=>$postdata['plugin_name']?$postdata['plugin_name']:'core',
	);
	if($postdata['fileddata'] && !$msglog['id']){
		foreach($postdata['fileddata'] as $pv){
            if($pv['type']==8 && $pv['data']){
                if(!preg_match("/^1[3456789]{1}\d{9}$/",$pv['data'])){
                    $return['error'] =1;
                    $return['message'] = '请输入正确的手机号码！';
                    exi($return);
                }
            }
            if($pv['type']==9 && $pv['data']){
                if(!filter_var($pv['data'], FILTER_VALIDATE_EMAIL)){
                    $return['error'] =1;
                    $return['message'] = '请输入正确的邮箱！';
                    exi($return);
                }
            }
			if($pv['type']==21){
				$pv['data']=$pv['data']?:array();
				$pv['data'][0]=$pv['data'][0]?:'粤';
				$pv['data'][1]=$pv['data'][1]?:'A';
				if(!$pv['data'][2]){
                    $return['error'] =1;
                    $return['message'] = '车牌号请完善！';
                    exi($return);
				}
			}
			if($pv['type']==22 && $pv['data']){
                $checkcode=cfc('tools_other')->checkcode($pv['data']);
				if(!$checkcode) {
					$return['error'] =1;
                    $return['message'] = '请填写正确的身份证！';
                    exi($return);
				}
            }
		}
		$data['uniacid']=$_SESSION['uniacid'];
		$data['uid']=$_SESSION['uid'];
		$data['formid']=$formid;
		$data['createtime']=TIMESTAMP;
		$data['type']=1;
		$data['aid']=$postdata['pageid'];
		pdo_insert('core_account_message',$data);
		$order_id=pdo_insertid();
		if($fee>0){
			$params=array(
				'id'=>$order_id,
				'title'=>'提交表单',
				'fee'=>$fee,
				//'pay_type'=>'wechat',//可选，此项作用为指定支付方式
				'no_type'=>array('hdfk'),//可选，此项作用为指定不允许使用哪种支付方式
				'back_url'=>'core/member.diyform/payresult',//必填，指定异步支付结果通知地址，采用系统标准URL书写格式
				'success_url'=>'core/member.diyform/',
				'error_url'=>'core/member.diyform/',
			);
			$payid=cfc('pay')->create_pay_order($params);
			pdo_update('core_account_message',array('pay_id'=>$payid,'price'=>$fee),array('id'=>$order_id));
			$res=array(
				'pay_id'=>$payid,
				'order_id'=>$order_id,
			);
			exi($res);
		}else{
			$return['message'] = '提交成功！';
			exi($return);
		}
	}elseif($msglog['id']){
		pdo_update('core_account_message',$data,array('uniacid'=>$_SESSION['uniacid'],'uid'=>$_SESSION['uid'],'formid'=>$formid));
		$return['message'] = '提交成功！';
		exi($return);
	}else{
		$return['error'] =1;
		$return['message'] = '提交数据为空！';
		exi($return);
	}
	
}
if($_RQ['op']=='payresult'){
	$payresult=$_SESSION['pay_result'];
	if($payresult['status']!=1 && $payresult['pay_type'] !='hdfk'){
		exi('支付失败。','error','core/member.diyform/');
	}
	$order=pdo_get('core_account_message',array('id'=>$payresult['id']));
	if($order['ispay'] ==1){
		exi('该订单已经支付！','error','core/member.diyform/');
	}
	if($order['price']!=$payresult['fee']){
		exi('支付失败！订单金额不匹配！'.$order['price'].'|'.$payresult['fee'],'error','core/member.diyform/');
	}
	$update=array(
		'paytype'=>$payresult['pay_type'],
		'transid'=>$payresult['transaction_id'],
		'status'=>1,
		'ispay'=>1
	);
	pdo_update('core_account_message',$update,array('id'=>$order['id']));
	exi('提交成功!');
}
